from fastapi import APIRouter, Request
from .base import RouterMeta
from src.constants.resultcode import ResultCode
from src.utils.response import ApiResponse
from src.utils.logger import get_logger
from src.schemas.video import DraftAddVideoRequest
from src.service.add_video import add_video_impl

logger = get_logger("capcut")
router = APIRouter()



@router.post("/add_video",name="添加视频")
async def add_video_to_draft(request: Request, video_request: DraftAddVideoRequest):
    """
    向草稿添加视频
    """
    try:
        draft_result = add_video_impl(
                draft_folder=video_request.draft_folder,
                video_url=video_request.video_url,
                width=video_request.width,
                height=video_request.height,
                start=video_request.start,
                end=video_request.end,
                target_start=video_request.target_start,
                draft_id=video_request.draft_id,
                transform_y=video_request.transform_y,
                scale_x=video_request.scale_x,
                scale_y=video_request.scale_y,
                transform_x=video_request.transform_x,
                speed=video_request.speed,
                track_name=video_request.track_name,
                relative_index=video_request.relative_index,
                duration=video_request.duration,
                transition=video_request.transition,  # Pass transition type parameter
                transition_duration=video_request.transition_duration,  # Pass transition duration parameter
                volume=video_request.volume,  # Pass volume parameter
                # Pass mask related parameters
                mask_type=video_request.mask_type,
                mask_center_x=video_request.mask_center_x,
                mask_center_y=video_request.mask_center_y,
                mask_size=video_request.mask_size,
                mask_rotation=video_request.mask_rotation,
                mask_feather=video_request.mask_feather,
                mask_invert=video_request.mask_invert,
                mask_rect_width=video_request.mask_rect_width,
                mask_round_corner=video_request.mask_round_corner,
                background_blur=video_request.background_blur
        )
        return ApiResponse.success(
            data=draft_result,
        )
    except Exception as e:
        logger.error(f"添加视频失败: {str(e)}", exc_info=True)
        return ApiResponse.error(
            result_code=ResultCode.ERROR,
            message=f"添加视频失败: {str(e)}",
        )

# 路由元数据配置
meta = RouterMeta(
    router=router,
    prefix="/video",
    tags=["视频"]
)